//
//  SHPingViewController.m
//  youjian03
//
//  Created by 常琼 on 14/11/10.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHPingViewController.h"
#import "UIView+Extension.h"

@interface SHPingViewController ()<UITextViewDelegate>
@property(nonatomic,assign) CGRect viewRect;

@property (weak, nonatomic) IBOutlet UILabel *pingtitle;

@property(nonatomic,assign) int point;

@property(nonatomic,copy) NSString *pingtext;//评价内容

@property(nonatomic,copy)NSString  *isHide;//是否匿名
@property (weak, nonatomic) IBOutlet UITextView *textfile;//评价内容
- (IBAction)nickPing:(UIButton *)sender;

@end

@implementation SHPingViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = [[NSBundle mainBundle] loadNibNamed:@"SHPingViewController" owner:self options:nil][0];
 
    
    //添加标题和左右按钮
    
    //"登录"标题
    UIButton *youjian =  [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 69, 17)];
    [youjian setTitle:@"评价服务" forState:UIControlStateNormal];
    [youjian setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
    
    [youjian setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    
    youjian.userInteractionEnabled = NO;
    
    self.navigationItem.titleView = youjian;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
   
    
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(sendPing)];
    
    [self.navigationItem.rightBarButtonItem setTintColor: [UIColor colorWithRed:0.399 green:0.400 blue:0.398 alpha:1.000]];
    
    
    
    
    self.navigationItem.rightBarButtonItem .enabled = NO;
    
    self.textfile.delegate = self;
    //添加工具条
    UIBarButtonItem *b1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    
    UIBarButtonItem *b2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done)];
    
    
    
    
    //给工具条添加按钮和相应方法
    
    NSArray *bArray = [NSArray arrayWithObjects: b1, b2, nil];
    
    UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 33)];//创建工具条对象
    
    tb.items = bArray;
    
    
    
    self.textfile.inputAccessoryView = tb;
    
 
    self.isHide = @"0";
    
    [self.view setFrame:CGRectMake(0, -20, 320, 320)];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        
    });
    
}
- (void)keyboardWillShow:(NSNotification *)notif {
    
   
    [UIView animateWithDuration:0.5 animations:^{
        
//        self.viewRect = self.view.frame;
        [self.view setY:-40];
        
     } completion:^(BOOL finish){
        
    }];
    
}

- (void)keyboardWillHide:(NSNotification *)notif {
    
    [UIView animateWithDuration:0.5 animations:^{
        HMLog(@"huiqu");
        [self.view setY:40];
    } completion:^(BOOL finish){
        
    }];
    
}
-(void)done
{
    [self.view endEditing:YES];
    
}

/**
 *  点击星星
 *
 */
- (IBAction)clickStar:(UIButton *)sender {
    
    
    for(int i = 1;i<=5 ;i++)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:i];
        button .selected =  NO;
    }
    if(sender.tag ==1)
    {
        self.pingtitle.text=@"非常不满意";
    }
    else if (sender .tag ==2)
    {
        self.pingtitle.text=@"不满意";
    }
    else if (sender .tag ==3)
    {
        self.pingtitle.text=@"一般";
    }
    else if (sender .tag ==4)
    {
        self.pingtitle.text=@"满意";
    }
    else if (sender .tag ==5)
    {
        self.pingtitle.text=@"非常满意";
    }
    
    for(int i = 1;i<=sender.tag ;i++)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:i];
        button .selected =  YES;
        
        
    }
    self.navigationItem.rightBarButtonItem .enabled = YES;
    
    self.point= sender.tag;//设置分数
    
    
    
}
/**
 *  发表按钮
 */
-(void)sendPing
{
    NSString *token = [SHReturnToken returnTokenNo];
 
    NSString *point = [NSString stringWithFormat:@"%d",self.point];
    

    
    if(self.textfile.text == nil)
    {
        self.pingtext = @"";
    }
    else
    {
        self.pingtext = self.textfile.text;
    }
    
    NSDictionary *dic  = @{@"anonymous":self.isHide,@"content":self.pingtext,@"starNum":point ,@"deliverId":self.orderId,@"token":token};
    
    
    
    
    [SHHTTPTOOL PostWithString:dic andAction:@"userCommentOffer" success:^(id responseObj) {
    
        [MBProgressHUD showSuccess:@"评价成功"];
        
        if ([self .delegate respondsToSelector:@selector(successPingWithRow:)])
        {
            [self.delegate successPingWithRow: self.row];
        }
        
        [self cancel];
    } failure:^(NSError *error) {
        [self cancel];
        
    }];}
-(void)cancel
{
    [self.view endEditing:NO];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
#pragma mark -UITextView

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    self.textfile .text= @"";
    self.textfile .textColor = [UIColor blackColor];
    
    
}



- (IBAction)nickPing:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (sender.selected ==YES)
    {
        
     self.isHide = @"1";
        
    }
    else
    {
        
    self .isHide = @"0";
    }
    
}
@end
